//
//  MessageFactory.swift
//  cangchu
//
//  Created by kang on 2021/11/26.
//

import Foundation

public class MessageFactory{
    
    private init(){}
    
    //心跳消息
    public static func createHearMessage(content:String)->Message{
        let message = Message()
        message.messageType = .HEARTBEAT
        message.deviceId = PushConfig.getLocalToken()
        message.content = content
        message.time = getTimeStr()
        return message
    }
    
    //回调消息
    public static func createCallBack(id:String,content:String)->Message{
        let message = Message()
        message.messageType = .TEXT
        message.content = content
        message.deviceId = PushConfig.getLocalToken()
        message.recordId = id
        message.time = getTimeStr()
        return message
    }
    
    //首次连接消息
    public static func createConnectMessage()->Message{
        let message = Message()
        message.messageType = .AUTH
        message.time = getTimeStr()
        message.deviceId = PushConfig.getLocalToken()

        let array = NSMutableDictionary()
        array.setValue(PushConfig.getLocalToken(), forKey: "deIdentify")
        array.setValue(PushConfig.meAppid, forKey: "deApplication")
        array.setValue(PushConfig.getLocalToken(), forKey: "facturerIdentify")//其他通道token
//        array.setValue("IOS", forKey: "deFacturer")//通道
        array.setValue(2, forKey: "deType")
        
        message.content = toJSonStr(element: array)
        return message
    }
    
    public static func createFeedBackMessage(id:String)->Message{
        let message = Message()
        message.deviceId = PushConfig.getLocalToken()
        message.time = getTimeStr()
        message.recordId = id
        message.messageType = .RECEIPT
        return message
    }
    
    //绑定消息
    public static func createBindMessage(account:String?, alias:String?, tags:[String]?)->Message{
        let message = Message()
        message.deviceId = PushConfig.getLocalToken()
        message.time = getTimeStr()
        message.messageType = .BIND
        
        let array = NSMutableDictionary()
        array.setValue("0", forKey: "isBind")//绑定:0，解绑:1
        if account != nil{
            array.setValue(account, forKey: "account")
        }
        if alias != nil{
            array.setValue(alias, forKey: "alias")
        }
        if tags != nil{
            array.setValue(tags, forKey: "tags")
        }
        array.setValue(PushConfig.getLocalToken(), forKey: "deviceId")
        
        message.content = toJSonStr(element: array)
        return message
    }
    
    /**
       type: 解绑类型 account:0, alias:1, tags:2
     */
    public static func createUnBindMessage(type:Int)->Message{
        let message = Message()
        message.deviceId = PushConfig.getLocalToken()
        message.time = getTimeStr()
        message.messageType = .BIND
        
        let array = NSMutableDictionary()
        array.setValue(1, forKey: "isBind")//绑定:0，解绑:1
        array.setValue(type, forKey: "unBindType")
        array.setValue(PushConfig.getLocalToken(), forKey: "deviceId")
        message.content = toJSonStr(element: array)
        return message
    }
    
    private static func getTimeStr()->String{
        let format = DateFormatter()
        format.dateFormat = "yyyy-MM-dd HH:mm:ss"
        return format.string(from: Date())
    }
}
